########################################################################
#  Wiizard - A Wii games manager
#  Copyright (C) 2023  CYBERDEViL
#
#  This file is part of Wiizard.
#
#  Wiizard is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Wiizard is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################



""" Constants """
SOURCE_LOCAL = 1  # Game file(s) are on mounted system HDD
SOURCE_PART  = 2  # Game file(s) are on a WBFS partition

FILE_TYPE_ISO  = 1
FILE_TYPE_WBFS = 2


DISC_IDS     = ["R", "S", "B", "E"]
REGION_STRS  = {
  "A": "All",
  "D": "German",
  "E": "USA",
  "F": "French",
  "I": "Italian",
  "J": "Japan",
  "K": "Korea",
  "L": "Japanese import to Europe",
  "M": "USA import to Europe",
  "N": "Japanese import to USA",
  "P": "Europe",
  "S": "Spanish",
  "Q": "Korea with Japanese language",
  "T": "Korea with English language"
}


class ID6:
  """ Get elements from a ID6 string """

  @staticmethod
  def getRegion(id6):
    return id6[3]

  @staticmethod
  def isValid(id6):
    """ Validate a id6 string """
    if len(id6) != 6:
      return False
    if id6[0] not in DISC_IDS:
      return False
    if id6[3] not in REGION_STRS:
      return False
    return True


class GameFileInfo:
  """ Base class to describe a game file and it's source """
  def __init__(self, id6, title, sourceType, sourcePath, fileType, fileSize, source):
    self.__id6        = id6
    self.__title      = title
    self.__sourceType = sourceType
    self.__sourcePath = sourcePath
    self.__fileType   = fileType
    self.__fileSize   = fileSize  # in MiB
    self.__source     = source    # source model

  def equals(self, other):
    if other.id6 != self.id6:
      return False

    if other.title != self.title:
      return False

    if other.fileType != self.fileType:
      return False

    if other.fileSize != self.fileSize:
      return False

    if other.sourceModel != self.sourceModel:
      return False

    return True

  @property
  def id6(self):
    return self.__id6

  @property
  def title(self):
    return self.__title

  @property
  def region(self):
    return ID6.getRegion(self.id6)

  @property
  def fileType(self):
    return self.__fileType

  @property
  def fileSize(self):
    return self.__fileSize

  @property
  def sourceType(self):
    return self.__sourceType

  @property
  def sourcePath(self):
    return self.__sourcePath

  @property
  def sourceModel(self):
    return self.__source

  def isValid(self):
    return ID6.isValid(self.id6)


class LocalGameFileInfo(GameFileInfo):
  """ For game files on local HDD """
  def __init__(self, id6, title, path, filename, fileType, fileSize, source):
    GameFileInfo.__init__(self, id6, title, SOURCE_LOCAL, path, fileType, fileSize, source)
    self.__filename = filename

  @property
  def filename(self):
    return self.__filename


class PartitionGameFileInfo(GameFileInfo):
  """ For game files on a WBFS partition """
  def __init__(self, id6, title, path, slot, fileType, fileSize, source):
    GameFileInfo.__init__(self, id6, title, SOURCE_PART, path, fileType, fileSize, source)
    self.__slot = slot

  @property
  def slot(self):
    return self.__slot
